<template>
	<view class="wrap">

		<uni-search-bar placeholder="搜索水果" cancelButton="none" bgColor="#f5f5f5" @confirm="search" />
		<view v-for="category in categoryList" >

			<uni-section v-if="category.child" :title="category.classLabel" type="line" padding>
				<swiper :class="getSwiperClass(category.child)" :indicator-dots="true">
					<swiper-item v-for="index of getPage(category.child)">
						<uni-grid :column="3" :showBorder="false" :highlight="true" >
							<uni-grid-item v-for="(item, index1) in category.child" v-if="isShow(index,index1)"
								:index="index1" :key="index1">
								<view class="grid-item-box" @click="categoryInfo(item)">
									<image :src="item.imageUrl" class="image" mode="aspectFill" />
									<text class="text">{{item.classLabel }}</text>
								</view>
							</uni-grid-item>
						</uni-grid>
					</swiper-item>
				</swiper>
			</uni-section>

		</view>

	</view>
</template>

<script>
	import {
		categoryTree
	} from '@/network/api/classification.js'
	export default {
		data() {
			return {
				categoryList: []
			}
		},
		onLoad() {
			//获取大分类以及大分类下所属的物品分类
			categoryTree().then(result => {
				this.categoryList = result.data
			}) 
			

		},
		methods: {
	
			getPage(child) {

				const page = Math.ceil(child.length / 9)
				return page
			},
			isShow(index, index1) {
				return index1 < index * 9 && index1 >= (index - 1) * 9
			},
			categoryInfo(item){
				console.log(item)
				uni.redirectTo({
					url:"/pages/classinfo/classinfo?classId="+item.classId,
				})
			},
			search(keyword) {
				console.log(keyword)
				uni.redirectTo({
					url:"/pages/searchlist/searchlist?keyword="+keyword.value
				})
			},
			getSwiperClass(child){
				const length = child.length
				console.log(length)
				if(length<=3){
					return "swiper1"
				}
				if(length>3&&length<=6){
					return "swiper2"
				}
				if(length>6){
					return "swiper3"
				}
			}
		}
	}
</script>

<style lang="scss">
	.content {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}

	.logo {
		height: 200rpx;
		width: 200rpx;
		margin-top: 200rpx;
		margin-left: auto;
		margin-right: auto;
		margin-bottom: 50rpx;
	}

	.text-area {
		display: flex;
		justify-content: center;
	}

	.title {
		font-size: 36rpx;
		color: #8f8f94;
	}

	.image {
		width: 40px;
		height: 40px;
	}

	.text {
		font-size: 14px;
		margin-top: 5px;
	}

	.example-body {
		/* #ifndef APP-NVUE */
		// display: block;
		/* #endif */
	}

	.grid-dynamic-box {
		margin-bottom: 15px;
	}

	.grid-item-box {
		flex: 1;
		// position: relative;
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex-direction: column;
		align-items: center;
		justify-content: center;
		padding: 15px 0;
	}

	.grid-item-box-row {
		flex: 1;
		// position: relative;
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex-direction: row;
		align-items: center;
		justify-content: center;
		padding: 15px 0;
	}

	.grid-dot {
		position: absolute;
		top: 5px;
		right: 15px;
	}

	.swiper {
		height: 370px;

	}
	.swiper1{
		height: 150px;
	}
	.swiper2{
		height: 260px;
	}
	.swiper3{
		height: 370px;
	}

	@media screen and (min-width: 768px) and (max-width: 1425px) {
		.swiper {
			height: 630px;
		}
	}

	@media screen and (min-width: 1425px) {
		.swiper {
			height: 830px;
		}
	}
</style>
